package com.clz.hyperit.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.ArticleDao;
import com.clz.hyperit.dao.ArticleTagDao;
import com.clz.hyperit.dao.PositionDao;
import com.clz.hyperit.dao.QuestionDao;
import com.clz.hyperit.dao.QuestionTagDao;
import com.clz.hyperit.dao.TagDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.ArticleTag;
import com.clz.hyperit.entity.Question;
import com.clz.hyperit.entity.QuestionTag;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.exception.ApplicationRunTimeException;
import com.clz.hyperit.service.TagService;
import com.clz.hyperit.vo.PageInfo;

@Service("tagService")
public class TagServiceImpl implements TagService {
	private static Logger logger = LoggerFactory
			.getLogger(TagServiceImpl.class);
	@Autowired
	private TagDao tagDao;

	@Autowired
	private ArticleTagDao articleTagDao;
	@Autowired
	private QuestionTagDao questionTagDao;

	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private QuestionDao questionDao;

	public Tag getByName(String name) {
		return tagDao.getByName(name);
	}
	public Tag findById(Integer id) {
		return tagDao.findById(id);
	}

	public Integer saveTag(Tag tag) {
		return tagDao.save(tag);
	}

	public void deleteTag(Tag tag) {
		tagDao.deleteById(tag.getId());
	}

	public void updateTag(Tag tag) {
		tagDao.updateWithNotNullProperties(tag);
	}

	public List<Tag> findTag() {
		return tagDao.findAll();
	}

	public List<Integer> saveTagArticle(Integer article_id, String name) {
		Tag tag = null;
		List<Integer> id_list = new ArrayList<Integer>();
		String[] TagName = name.split(" ");
		for (int i = 0; i < TagName.length; i++) {
			tag = tagDao.getByName(TagName[i]);
			// 如果不存在该标签 就创建
			if (tag == null) {
				tag = new Tag();
				tag.setArticle(0);
				tag.setName(TagName[i]);
				tag.setQuestion(0);
				tagDao.save(tag);
			}
			tag.setArticle(tag.getArticle() + 1);
			ArticleTag at = new ArticleTag();
			at.setArticle_id(article_id);
			at.setTag_id(tag.getId());
			id_list.add(articleTagDao.save(at));
		}
		return id_list;
	}

	public List<Tag> findTagByArticle(Integer id) {
		List<ArticleTag> at_list = articleTagDao.findByArticleId(id);
		List<Tag> tag_list = new ArrayList<Tag>();
		for (ArticleTag at : at_list) {
			tag_list.add(tagDao.get(at.getTag_id()));
		}
		return tag_list;
	}

	public void deleteByArticle(Integer article_id) {
		articleTagDao.deleteByArticleID(article_id);

	}

	public List<Integer> saveTagQuestion(Integer question_id, String name) {
		Tag tag = null;
		List<Integer> id_list = new ArrayList<Integer>();
		String[] TagName = name.split(" ");
		for (int i = 0; i < TagName.length; i++) {
			tag = tagDao.getByName(TagName[i]);
			// 如果不存在该标签 就创建
			if (tag == null) {
				tag = new Tag();
				tag.setArticle(0);
				tag.setName(TagName[i]);
				tag.setQuestion(0);
				tagDao.save(tag);
			}
			tag.setQuestion(tag.getQuestion() + 1);
			QuestionTag qt = new QuestionTag();
			qt.setQuestion_id(question_id);
			qt.setTag_id(tag.getId());
			id_list.add(questionTagDao.save(qt));
		}
		return id_list;
	}

	public List<Tag> findTagByQuestion(Integer id) {
		List<QuestionTag> qt_list = questionTagDao.findByQuestionId(id);
		List<Tag> tag_list = new ArrayList<Tag>();
		for (QuestionTag qt : qt_list) {
			tag_list.add(tagDao.get(qt.getTag_id()));
		}
		return tag_list;
	}

	public List<Article> getArticle(Integer tag_id,PageInfo pageInfo) {
		List<ArticleTag> at_list = articleTagDao.getByTagid(tag_id,pageInfo);
		List<Article> article_list = new ArrayList<Article>();
		for (ArticleTag at : at_list) {
			Article article = articleDao.get(at.getArticle_id());
			article_list.add(article);
		}
		return article_list;
	}

	public List<Question> getQuestion(Integer tag_id,PageInfo pageInfo) {
		List<QuestionTag> qt_list = questionTagDao.getByQuestionId(tag_id,pageInfo);
		List<Question> question_list = new ArrayList<Question>();
		for (QuestionTag qt : qt_list) {
			Question question = questionDao.get(qt.getQuestion_id());
			question_list.add(question);
		}
		return question_list;
	}

	public List<Tag> findTagByReading(PageInfo pageInfo) {
		List<Tag> tag_list = tagDao.findAll(pageInfo);
		Tag max = null;
		for (int i = 0; i < tag_list.size(); i++) {
			for (int j = 0; j < tag_list.size()-i; j++) {
					if(tag_list.get(i).count()<tag_list.get(i+1).count()){
						max = tag_list.get(i+1);
						tag_list.set(i+1,tag_list.get(i));
						tag_list.set(i,max);
					}
			}
		}
		return tag_list;
	}
	public List<Tag> findTagByTime(PageInfo pageInfo) {
		return tagDao.findAll(pageInfo);
	}
	public List<Tag> findTagBySearch(PageInfo pageInfo, String tag) {
		return tagDao.findBySearch(pageInfo,tag);
	}
	public List<Tag> findTagByArticleNum(PageInfo pageInfo) {
		return tagDao.findByArticleNum(pageInfo);
	}
	public List<Tag> findTagByQuestionNum(PageInfo pageInfo) {
		return tagDao.findByQuestionNum(pageInfo);
	}
	
}
